/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.zimbra.common.mime.ContentDisposition;
/*     */ import com.zimbra.common.mime.MimeDetect;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.ByteUtil.PositionInputStream;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.activation.DataSource;
/*     */ import javax.mail.BodyPart;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import javax.mail.util.ByteArrayDataSource;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class UUEncodeConverter
/*     */   extends MimeVisitor
/*     */ {
/*     */   protected boolean visitMultipart(MimeMultipart mmp, MimeVisitor.VisitPhase visitKind)
/*     */   {
/*  48 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean visitBodyPart(MimeBodyPart mbp)
/*     */   {
/*  53 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean visitMessage(MimeMessage mm, MimeVisitor.VisitPhase visitKind)
/*     */     throws MessagingException
/*     */   {
/*  59 */     if (visitKind != MimeVisitor.VisitPhase.VISIT_END) {
/*  60 */       return false;
/*     */     }
/*  62 */     MimeMultipart mmp = null;
/*     */     try
/*     */     {
/*  65 */       if (!mm.isMimeType("text/plain")) {
/*  66 */         return false;
/*     */       }
/*     */       
/*  69 */       String cte = mm.getHeader("Content-Transfer-Encoding", null);
/*  70 */       if (cte != null) {
/*  71 */         cte = cte.trim().toLowerCase();
/*  72 */         if ((!cte.equals("7bit")) && (!cte.equals("8bit")) && (!cte.equals("binary"))) {
/*  73 */           return false;
/*     */         }
/*     */       }
/*  76 */       List<UUDecodedFile> uufiles = null;
/*     */       
/*     */ 
/*  79 */       ByteUtil.PositionInputStream is = null;
/*     */       long size;
/*     */       try {
/*  82 */         is = new ByteUtil.PositionInputStream(new BufferedInputStream(mm.getInputStream()));
/*  83 */         for (int c = is.read(); c != -1; 
/*     */             
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 110 */             goto 453)
/*     */         {
/*  84 */           long start = is.getPosition() - 1L;
/*     */           
/*  86 */           if ((c == 98) && ((c = is.read()) == 101) && ((c = is.read()) == 103) && ((c = is.read()) == 105) && ((c = is.read()) == 110) && (((c = is.read()) == 32) || (c == 9)) && (Character.isDigit(c = is.read())) && (Character.isDigit(c = is.read())) && (Character.isDigit(c = is.read())) && (((c = is.read()) == 32) || (c == 9)))
/*     */           {
/*     */ 
/*     */ 
/*     */ 
/*  91 */             StringBuilder sb = new StringBuilder();
/*  92 */             while (((c = is.read()) != 13) && (c != 10) && (c != -1))
/*  93 */               sb.append((char)c);
/*  94 */             String filename = FileUtil.trimFilename(sb.toString().trim());
/*  95 */             if ((c != -1) && (filename.length() > 0)) {
/*  96 */               if (uufiles == null)
/*  97 */                 uufiles = new ArrayList(3);
/*     */               try {
/*  99 */                 uufiles.add(new UUDecodedFile(is, filename, start));
/*     */                 
/* 101 */                 if ((uufiles.size() == 1) && (this.mCallback != null) && (!this.mCallback.onModification())) {
/* 102 */                   return false;
/*     */                 }
/*     */               } catch (IOException ioe) {}
/*     */             }
/*     */           }
/* 107 */           while ((c != 13) && (c != 10) && (c != -1))
/* 108 */             c = is.read();
/* 109 */           if ((c == 13) || (c == 10))
/* 110 */             c = is.read();
/*     */         }
/* 112 */         size = is.getPosition();
/*     */       } finally {
/* 114 */         ByteUtil.closeStream(is);
/*     */       }
/*     */       
/* 117 */       if ((uufiles == null) || (uufiles.isEmpty())) {
/* 118 */         return false;
/*     */       }
/*     */       
/* 121 */       mmp = new ZMimeMultipart("mixed");
/* 122 */       for (UUDecodedFile uu : uufiles) {
/* 123 */         MimeBodyPart mbp = new ZMimeBodyPart();
/* 124 */         mbp.setHeader("Content-Type", uu.getContentType());
/* 125 */         mbp.setHeader("Content-Disposition", new ContentDisposition("attachment").setParameter("filename", uu.getFilename()).toString());
/* 126 */         mbp.setDataHandler(new DataHandler(uu.getDataSource()));
/* 127 */         mmp.addBodyPart(mbp);
/*     */         
/* 129 */         size -= uu.getEndOffset() - uu.getStartOffset();
/*     */       }
/*     */       
/*     */ 
/* 133 */       InputStream isOrig = null;
/*     */       try {
/* 135 */         isOrig = mm.getInputStream();
/* 136 */         long offset = 0L;
/* 137 */         ByteArrayOutputStream baos = new ByteArrayOutputStream((int)size);
/* 138 */         byte[] buffer = new byte[' '];
/* 139 */         for (UUDecodedFile uu : uufiles) { long numRead;
/* 140 */           for (long count = uu.getStartOffset() - offset; 
/* 141 */               (count > 0L) && ((numRead = isOrig.read(buffer, 0, (int)Math.min(count, 8192L))) >= 0L); 
/* 142 */               count -= numRead) { baos.write(buffer, 0, (int)numRead);
/*     */           }
/* 144 */           isOrig.skip(uu.getEndOffset() - uu.getStartOffset());
/* 145 */           offset = uu.getEndOffset();
/*     */         }
/* 147 */         ByteUtil.copy(isOrig, true, baos, true);
/*     */         
/* 149 */         Object mbp = new ZMimeBodyPart();
/* 150 */         ((MimeBodyPart)mbp).setDataHandler(new DataHandler(new ByteArrayDataSource(baos.toByteArray(), "text/plain")));
/* 151 */         mmp.addBodyPart((BodyPart)mbp, 0);
/*     */       } finally {
/* 153 */         ByteUtil.closeStream(isOrig);
/*     */       }
/*     */     } catch (MessagingException e) {
/* 156 */       ZimbraLog.extensions.warn("exception while uudecoding message part; skipping part", e);
/* 157 */       return false;
/*     */     } catch (IOException e) {
/* 159 */       ZimbraLog.extensions.warn("exception while uudecoding message part; skipping part", e);
/* 160 */       return false;
/*     */     }
/*     */     
/*     */ 
/* 164 */     mm.setContent(mmp);
/* 165 */     mm.setHeader("Content-Type", mmp.getContentType() + "; generated=true");
/* 166 */     return true;
/*     */   }
/*     */   
/*     */   private static class UUDecodedFile {
/*     */     private final String mFilename;
/*     */     private String mContentType;
/*     */     private final long mStartOffset;
/*     */     private final long mEndOffset;
/*     */     private final byte[] mContent;
/*     */     
/* 176 */     UUDecodedFile(ByteUtil.PositionInputStream is, String filename, long start) throws IOException { this.mFilename = filename;
/* 177 */       this.mStartOffset = start;
/*     */       
/* 179 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 180 */       int length = -1;int bits = 0;int acc = 0;int c = is.read();
/* 181 */       while ((c == 114) || (c == 10)) {
/* 182 */         c = is.read();
/*     */       }
/* 184 */       for (; c != -1; 
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 212 */           goto 240)
/*     */       {
/* 186 */         if ((length == 0) && (c == 101)) {
/* 187 */           is.mark(3);
/* 188 */           if ((is.read() == 110) && (is.read() == 100))
/*     */             break;
/* 190 */           is.reset();
/*     */         }
/* 192 */         length = c - 32 & 0x3F;
/*     */         
/*     */ 
/* 195 */         for (int decoded = 0; decoded < length;) {
/* 196 */           if ((c = is.read()) == -1)
/* 197 */             throw new IOException("unexpected eof during uuencoded attachment");
/* 198 */           acc = acc << 6 | c - 32 & 0x3F;
/* 199 */           bits += 6;
/* 200 */           if (bits >= 8)
/*     */           {
/* 202 */             bits -= 8;
/* 203 */             baos.write(acc >> bits);
/* 204 */             acc &= 255 >> 8 - bits;
/* 205 */             decoded++;
/*     */           }
/*     */         }
/*     */         
/* 209 */         while (((c = is.read()) != 13) && (c != 10) && (c != -1)) {}
/*     */         
/* 211 */         if ((c == 13) || (c == 10)) {
/* 212 */           c = is.read();
/*     */         }
/*     */       }
/*     */       
/* 216 */       this.mContent = baos.toByteArray();
/* 217 */       this.mContentType = MimeDetect.getMimeDetect().detect(this.mFilename, this.mContent);
/* 218 */       if (this.mContentType == null) {
/* 219 */         this.mContentType = "application/octet-stream";
/*     */       }
/*     */       
/* 222 */       this.mEndOffset = is.getPosition();
/*     */     }
/*     */     
/* 225 */     long getStartOffset() { return this.mStartOffset; }
/* 226 */     long getEndOffset() { return this.mEndOffset; }
/*     */     
/* 228 */     String getContentType() { return this.mContentType; }
/* 229 */     String getFilename() { return this.mFilename; }
/*     */     
/*     */     DataSource getDataSource() {
/* 232 */       ByteArrayDataSource bads = new ByteArrayDataSource(this.mContent, this.mContentType);
/* 233 */       bads.setName(this.mFilename);
/* 234 */       return bads;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/UUEncodeConverter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */